"""
项目的主应用
"""
from flask import Flask
from flask_cors import CORS
from flask_restful import Api

from common import db, cache
from practise_flask.account_resource.account import account_bp
from practise_flask.channel_resource.channel import channel_bp
from common.utils.middlewares import jwt_authentiction


def create_flask_app(config):
    """
    创建工厂函数，封装app
    :param config:
    """
    # 实例化app对象
    app = Flask(__name__)
    # 加载配置  需要在db初始化之前
    app.config.from_object(config)

    # 初始化db
    db.init_app(app)
    # 注册蓝图
    app.register_blueprint(account_bp)
    app.register_blueprint(channel_bp)

    app.before_request(jwt_authentiction)
    # 跨域配置
    core = CORS(app, resource={'/api': {'origins': '*'}})

    # 初始化cache
    cache.init_app(app)

    api = Api(app)
    return app


